<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\doctor\controller;

use think\Db;
use cmf\controller\RestBaseController;
use think\Config;
use think\Cache;
use \Qcloud\sms\SmsSingleSender AS sms;

class LoginController extends RestBaseController
{

    
    public $data;
    public function __construct(){
        $this->data = input();
    }
    /**
     * [index description]
     * @return [type] [description]
     */
    public function login(){
        if (!isset($this->data['tel'])||empty($this->data['tel'])) {
            $this->error('请输入手机号码！');
        }
        $doctor = Db::table('doctor')
            ->where(['tel'=>$this->data['tel']])
            ->field('id,name,headimg')
            ->find();
        if(!$doctor){
            $helper = Db::table('helper')->where(['tel'=>$this->data['tel']])->find();
            if(!$helper){
                $this->error('账号不存在！');
            }
            $doctor = Db::table('doctor')->where(['id'=>$helper['doc_id']])->find();
            $type = 3; //助手
        }else{
            $type = 2; //医生
        }
        if (!isset($this->data['code'])||empty($this->data['code'])) {
            $this->error('请填写验证码！');
        }
        $code = Db::table('doctor_login_code')
                ->where(['code'=>$this->data['code'],'tel'=>$this->data['tel']])
                ->find();
        if(!$code){
            $this->error('验证码有误！');
        }
        if(time() > $code['extime']){
            $this->error('验证码过期,请重新获取！');
        }
        $token = time().'wech'.$this->data['tel'].'type'.$type;
        Cache::set($token,$token,3600);
        $arr = array('token'=>$token,'type'=>$type,'userInfo'=>$doctor);
        $this->success('登陆成功!',$arr);
    }

    /*
    获取用户openid
     */
    public function getopenid(){
        if (!isset($this->data['code'])||empty($this->data['code'])) {
            $this->error('缺少:code');
        }
        if (!isset($this->data['appid'])||empty($this->data['appid'])) {
            $this->error('缺少:appid');
        }
        $appid = $this->data['appid'];//代管的小程序ID
        $code = $this->data['code']; //js_code
        $component_appid = Config::get('appId'); //第三方的appid
        $userid = Db::table('minipro_copy')->where(['appid'=>$appid])->value('merchant_id');
        $access_token = getAuthorizerAccessToken($userid);
        $url = 'https://api.weixin.qq.com/sns/component/jscode2session?appid='.$appid.'&js_code='.$code.'&grant_type=authorization_code&component_appid='.$component_appid.'&component_access_token='.$access_token;
        $openid = post($url,[]);
        $getopenid = json_decode($openid,true);
        if(!isset($getopenid['openid'])){
            $this->error($getopenid['errmsg']);
        }
        $this->success('获取成功!',$getopenid);
    }
    
    /**
     * [chick_session 检测session是否过期]
     * @return [type] [description]
     */
    public function chick_token(){
        $token =  input('token') ? input('token') : $_SERVER['HTTP_TOKEN'];
        if(!$token){
            $this->error('缺少:token');
        }
        if(!Cache::get($token)){
            $this->error('token已过期');
        }else{
            $this->success('token:有效');
        }
    }

    /**
     * [loginOut description]
     * @return [type] [description]
     */
    public function loginOut(){
        $token =  input('token') ? input('token') : $_SERVER['HTTP_TOKEN'];
        if(!$token){
            $this->error('缺少:token');
        }
        if(Cache::rm($token)){
            $this->success('退出成功!');
        }
        $this->error('退出失败!');
    }
}